package com.github.aloxc.plugin.restplus.resolver;


import com.github.aloxc.plugin.restplus.annotations.PathMappingAnnotation;
import com.github.aloxc.plugin.restplus.annotations.SpringControllerAnnotation;
import com.github.aloxc.plugin.restplus.annotations.SpringRequestMethodAnnotation;
import com.github.aloxc.plugin.restplus.annotations.SpringRequestParamAnnotations;
import com.github.aloxc.plugin.restplus.common.Consts;
import com.github.aloxc.plugin.restplus.common.DesUtil;
import com.github.aloxc.plugin.restplus.common.PsiAnnotationSearchUtil;
import com.github.aloxc.plugin.restplus.component.ServiceNode;
import com.github.aloxc.plugin.restplus.entity.*;
import com.github.aloxc.plugin.restplus.method.RequestPath;
import com.github.aloxc.plugin.restplus.navigation.action.RestServiceItem;
import com.github.aloxc.plugin.restplus.spring.SpringRequestMappingAnnotationHelper;
import com.github.aloxc.plugin.restplus.utils.Log;
import com.github.aloxc.plugin.restplus.utils.StringUtils;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.impl.java.stubs.index.JavaAnnotationIndex;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.kotlin.asJava.LightClassUtilsKt;
import org.jetbrains.kotlin.idea.stubindex.KotlinAnnotationsIndex;
import org.jetbrains.kotlin.psi.*;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static com.github.aloxc.plugin.restplus.annotations.SpringRequestParamAnnotations.REQUEST_BODY;

/**
 * @author leero
 */
public class SpringResolver extends BaseServiceResolver {
    public SpringResolver(Module module) {
        myModule = module;
    }

    public SpringResolver(Project project) {
        myProject = project;

    }


    //Note: 当Controller 类上没有标记@RequestMapping 注解时，方法上的@RequestMapping 都是绝对路径。
    /*@Override
    public List<RestServiceItem> getServiceItemList(PsiMethod psiMethod) {
        List<RestServiceItem> itemList = new ArrayList<>();

        RequestPath[] methodRequestPaths = RequestMappingAnnotationHelper.getRequestPaths(psiMethod);

        //TODO:  controller 没有设置requestMapping，默认“/”
        // TODO : controller 设置了(rest)controller 所有方法未指定 requestMapping，所有方法均匹配； 存在方法指定 requestMapping，则只解析这些方法；
//        String[] controllerPaths = RequestMappingAnnotationHelper.getRequestPaths(psiMethod.getContainingClass());
        List<RequestPath> classRequestPaths = RequestMappingAnnotationHelper.getRequestPaths(psiMethod.getContainingClass());
        for (RequestPath classRequestPath : classRequestPaths) {
            for (RequestPath methodRequestPath : methodRequestPaths) {
                String path =  classRequestPath.getPath();
//                String path = tryReplacePlaceholderValueInPath( classRequestPath.getPath() );

                RestServiceItem item = createRestServiceItem(psiMethod, path, methodRequestPath);
                itemList.add(item);
            }
        }

        return itemList;
    }*/


    /*
    private String  tryReplacePlaceholderValueInPath(String path) {
        if (!path.contains("${")) return path;

        StringBuilder cleanerPath = new StringBuilder();

        for (String s : path.split("\\$\\{")) {
            if (s.contains("}")) {
                String placeholder = s.substring(0, s.indexOf("}"));
                String theOther = s.substring(s.indexOf("}")+1);

                String propertyValue = propertiesHandler.getProperty(placeholder);

                cleanerPath.append(StringUtils.isEmpty(propertyValue) ? s : propertyValue.trim()).append(theOther);
            } else {
                cleanerPath.append(s);
            }
        }
        return cleanerPath.toString();
    }*/


/*    @NotNull
//    @Override
    public List<PsiMethod> getServicePsiMethodList(Project project, GlobalSearchScope globalSearchScope) {
        List<PsiMethod> psiMethodList = new ArrayList<>();
        //TODO : 这块需要改写，支持更多注解方式。
        // TODO: 这种实现的局限了其他方式实现的url映射（xml（类似struts），webflux routers）
        SpringControllerAnnotation[] supportedAnnotations = SpringControllerAnnotation.values();
        //        for (SpringControllerAnnotation controllerAnnotations : SpringControllerAnnotation.values()) {
        for (PathMappingAnnotation controllerAnnotation : supportedAnnotations) {

            // 标注了 (Rest)Controller 注解的类，即 Controller 类
            Collection<PsiAnnotation> psiAnnotations = JavaAnnotationIndex.getInstance().get(controllerAnnotation.getShortName(), project, globalSearchScope);
            for (PsiAnnotation psiAnnotation : psiAnnotations) {
                PsiModifierList psiModifierList = (PsiModifierList) psiAnnotation.getParent();
                PsiElement psiElement = psiModifierList.getParent();

                *//*if (!(psiElement instanceof PsiClass)) continue; // RestController annotation 只出现在class*//*
                PsiClass psiClass = (PsiClass) psiElement;
                PsiMethod[] psiMethods = psiClass.getMethods();
                // FIXME: 这里应该只包含 设置了，requstmapping的方法，除非所有方法都没标注 requstmapping
                if (psiMethods == null) {
                    continue;
                }

                for (PsiMethod psiMethod : psiMethods) {
                    // todo: 没有处理同时标注了 GET 和 POST 两种方法的方法，定义一个 RequestMapping 类{method，path}
                    psiMethodList.add(psiMethod);
                }
            }

        }
        return psiMethodList;
    }*/

    @Override
    public List<RestServiceItem> getRestServiceItemList(Project project, GlobalSearchScope globalSearchScope) {
        List<RestServiceItem> itemList = new ArrayList<>();
        // TODO: 这种实现的局限了其他方式实现的url映射（xml（类似struts），webflux routers）
        SpringControllerAnnotation[] supportedAnnotations = SpringControllerAnnotation.values();
        for (PathMappingAnnotation controllerAnnotation : supportedAnnotations) {
            try {
                // java: 标注了 (Rest)Controller 注解的类，即 Controller 类
                Collection<PsiAnnotation> psiAnnotations = JavaAnnotationIndex.getInstance().get(controllerAnnotation.getShortName(), project, globalSearchScope);
                for (PsiAnnotation psiAnnotation : psiAnnotations) {
                    PsiModifierList psiModifierList = (PsiModifierList) psiAnnotation.getParent();
                    PsiElement psiElement = psiModifierList.getParent();

                    /*if (!(psiElement instanceof PsiClass)) continue; // RestController annotation 只出现在 class*/
                    PsiClass psiClass = (PsiClass) psiElement;
                    if (null == psiClass.getModifierList().findAnnotation(SpringControllerAnnotation.CONTROLLER.getQualifiedName()) && null == psiClass.getModifierList().findAnnotation(SpringControllerAnnotation.REST_CONTROLLER.getQualifiedName())) {
                        continue;
                    }
                    List<RestServiceItem> serviceItemList = getServiceItemList(psiClass);
                    itemList.addAll(serviceItemList);
                }
            } catch (Throwable exe) {
                Writer result = new StringWriter();
                PrintWriter printWriter = new PrintWriter(result);
                exe.printStackTrace(printWriter);
                String msg = result.toString();
                msg.replaceAll("\\\\r\\\\n", "<br>");
//                PopupBalloon.showNotification(project, "开始getRestServiceItemList异常<br>" + msg, false);
                Log.log(SpringResolver.class.getName() + "-" + project.getName(), msg);
            }


            Collection<KtAnnotationEntry> ktAnnotationEntries = KotlinAnnotationsIndex.getInstance().get(controllerAnnotation.getShortName(), project, globalSearchScope);

            for (KtAnnotationEntry ktAnnotationEntry : ktAnnotationEntries) {
                KtClass ktClass = (KtClass) ktAnnotationEntry.getParent().getParent();

                List<RequestPath> classRequestPaths = getRequestPaths(ktClass);

                List<KtNamedFunction> ktNamedFunctions = getKtNamedFunctions(ktClass);
                for (KtNamedFunction fun : ktNamedFunctions) {
                    List<RequestPath> requestPaths = getRequestPaths(fun);

                    for (RequestPath classRequestPath : classRequestPaths) {
                        for (RequestPath requestPath : requestPaths) {
                            requestPath.concat(classRequestPath);
                            PsiClass psiClass = LightClassUtilsKt.toLightClass(ktClass);
                            RestServiceItem restServiceItem = createRestServiceItem(psiClass, fun, "", requestPath, false);
                            if(restServiceItem.getPsiMethod() == null){
                                restServiceItem.setPsiMethod(LightClassUtilsKt.toLightMethods(restServiceItem.getPsiElement()).get(0));
                            }
                            itemList.add(restServiceItem);
                        }
                    }
                }
/*
                PsiClass psiClass = LightClassUtilsKt.toLightClass(ktClass);
                itemList.addAll(getServiceItemList(psiClass));*/

            }

        }
        return itemList;
    }

    @Override
    public List<RestServiceItem> getServiceItemList(PsiClass psiClass) {

        PsiMethod[] psiMethods = psiClass.getMethods();
        if (psiMethods == null) {
            return new ArrayList<>();
        }
        List<RestServiceItem> itemList = new ArrayList<>();
        try {
            List<RequestPath> classRequestPaths = SpringRequestMappingAnnotationHelper.getRequestPaths(psiClass);
            for (PsiMethod psiMethod : psiMethods) {
                RequestPath[] methodRequestPaths = SpringRequestMappingAnnotationHelper.getRequestPaths(psiMethod);
                PsiParameterList parameterList = psiMethod.getParameterList();
                PsiParameter[] parameters = parameterList.getParameters();
                boolean foundRequestBody = false;
                for (PsiParameter psiParameter : parameters) {
                    PsiModifierList modifierList = psiParameter.getModifierList();
                    foundRequestBody = modifierList.findAnnotation(REQUEST_BODY.getQualifiedName()) != null;

                }
                for (RequestPath classRequestPath : classRequestPaths) {
                    for (RequestPath methodRequestPath : methodRequestPaths) {
                        String path = classRequestPath.getPath();
                        RestServiceItem item = createRestServiceItem(psiClass,psiMethod, path, methodRequestPath, foundRequestBody);
                        itemList.add(item);
                    }
                }
            }
        } catch (ProcessCanceledException e) {
            Log.log("SpringResolver", e.getMessage());
        }
        return itemList;
    }

    private List<KtNamedFunction> getKtNamedFunctions(KtClass ktClass) {
        List<KtNamedFunction> ktNamedFunctions = new ArrayList<>();
        List<KtDeclaration> declarations = ktClass.getDeclarations();

        for (KtDeclaration declaration : declarations) {
            if (declaration instanceof KtNamedFunction) {
                KtNamedFunction fun = (KtNamedFunction) declaration;
                ktNamedFunctions.add(fun);

            }
        }
        return ktNamedFunctions;
    }

    private List<RequestPath> getRequestPaths(KtClass ktClass) {
        String defaultPath = "/";
        //方法注解
        List<KtAnnotationEntry> annotationEntries = ktClass.getModifierList().getAnnotationEntries();

        List<RequestPath> requestPaths = getRequestMappings(defaultPath, annotationEntries);
        return requestPaths;
    }

    private List<RequestPath> getRequestPaths(KtNamedFunction fun) {
//        String methodBody = fun.getBodyExpression().getText();// 方法体
//        String defaultPath = fun.getName();
        String defaultPath = "/";
        //方法注解
        if(fun.getModifierList() == null){
            return new ArrayList<>();
        }
        List<KtAnnotationEntry> annotationEntries = fun.getModifierList().getAnnotationEntries();
        List<RequestPath> requestPaths = getRequestMappings(defaultPath, annotationEntries);
        return requestPaths;
    }

    private List<RequestPath> getRequestMappings(String defaultPath, List<KtAnnotationEntry> annotationEntries) {
        List<RequestPath> requestPaths = new ArrayList<>();
        for (KtAnnotationEntry entry : annotationEntries) {
//            List<RequestPath> requestMappings = getRequestMappings(defaultPath, entry);
            List<RequestPath> requestMappings = getRequestMappings(defaultPath, entry);
            requestPaths.addAll(requestMappings);
        }
        return requestPaths;
    }

   /* private List<RequestPath> getRequestMappings(String defaultPath, KtAnnotationEntry entry) {
        List<RequestPath> requestPaths = new ArrayList<>();
        List<String> methodList = new ArrayList<>();
        List<String> pathList = new ArrayList<>();

        String annotationName = entry.getCalleeExpression().getText();
        SpringRequestMethodAnnotation requestMethodAnnotation = SpringRequestMethodAnnotation.getByShortName(annotationName);
        if (requestMethodAnnotation == null) {
            return new ArrayList<>();
        }

        if (requestMethodAnnotation.methodName() != null) {
            methodList.add(requestMethodAnnotation.methodName());
        } else {
            // 下面循环获取
        }

        //注解参数值
        KtValueArgumentList valueArgumentList = entry.getValueArgumentList();
        // 只有注解，没有参数
        if (valueArgumentList != null) {
            List<KtValueArgument> arguments = valueArgumentList.getArguments();


            for (int i = 0; i < arguments.size(); i++) {
                KtValueArgument ktValueArgument = arguments.get(i);
                KtValueArgumentName argumentName = ktValueArgument.getArgumentName();

                KtExpression argumentExpression = ktValueArgument.getArgumentExpression();
                if (argumentName == null || argumentName.getText().equals("value") || argumentName.getText().equals("path")) {
                    // array, kotlin 1.1-
                    if (argumentExpression.getText().startsWith("arrayOf")) {
                        List<KtValueArgument> pathValueArguments = ((KtCallExpression) argumentExpression).getValueArguments();
                        for (KtValueArgument pathValueArgument : pathValueArguments) {
                            pathList.add(pathValueArgument.getText().replace("\"", ""));
                        }
                        // array, kotlin 1.2+
                    } else if (argumentExpression.getText().startsWith("[")) {
                        List<KtExpression> innerExpressions = ((KtCollectionLiteralExpression) argumentExpression).getInnerExpressions();
                        for (KtExpression ktExpression : innerExpressions) {
                            pathList.add(ktExpression.getText().replace("\"", ""));
                        }
                    } else {
                        // 有且仅有一个value
                        PsiElement[] paths = ktValueArgument.getArgumentExpression().getChildren();
//                            Arrays.stream(paths).forEach(p -> pathList.add(p.getText()));
                        pathList.add(paths.length==0? "" : paths[0].getText());
                    }
                    //TODO
                    continue;
                }

                String attribute = "method";
                if (argumentName.getText().equals(attribute)) {

                    // array, kotlin 1.1-
                    if (argumentExpression.getText().startsWith("arrayOf")) {
                        List<KtValueArgument> pathValueArguments = ((KtCallExpression) argumentExpression).getValueArguments();
                        for (KtValueArgument pathValueArgument : pathValueArguments) {
                            methodList.add(pathValueArgument.getText().replace("\"", ""));
                        }
                        // array, kotlin 1.2+
                    } else if (argumentExpression.getText().startsWith("[")) {
                        List<KtExpression> innerExpressions = ((KtCollectionLiteralExpression) argumentExpression).getInnerExpressions();
                        for (KtExpression ktExpression : innerExpressions) {
                            methodList.add(ktExpression.getText().replace("\"", ""));
                        }
                    } else {
                        // 有且仅有一个value
                        PsiElement[] paths = ktValueArgument.getArgumentExpression().getChildren();
//                            Arrays.stream(paths).forEach(p -> methodList.add(p.getText()));
                        methodList.add(paths.length==0? "" : paths[0].getText());
                    }

                }
            }
        } else {
            pathList.add(defaultPath);
            //method = "GET";
        }

        if (methodList.size() > 0) {
            for (String method : methodList) {
                for (String path : pathList) {
                    requestPaths.add(new RequestPath(path, method));
                }
            }
        } else {
            for (String path : pathList) {
                requestPaths.add(new RequestPath(path, null));
            }
        }

        return requestPaths;
    }*/


    // TODO Kotlin
    private List<RequestPath> getRequestMappings(String defaultPath, KtAnnotationEntry entry) {
        List<RequestPath> requestPaths = new ArrayList<>();
        List<String> methodList = new ArrayList<>();
        List<String> pathList = new ArrayList<>();

        String annotationName = entry.getCalleeExpression().getText();
        SpringRequestMethodAnnotation requestMethodAnnotation = SpringRequestMethodAnnotation.getByShortName(annotationName);
        if (requestMethodAnnotation == null) {
            return new ArrayList<>();
        }

        if (requestMethodAnnotation.methodName() != null) { // GetMapping PostMapping ...
            methodList.add(requestMethodAnnotation.methodName());
        } else {
            methodList.addAll(getAttributeValues(entry, "method")); // RequestMapping
        }

        //注解参数值
//        KtValueArgumentList valueArgumentList = entry.getValueArgumentList();
        if (entry.getValueArgumentList() != null) {
            List<String> mappingValues = getAttributeValues(entry, null);
            if (!mappingValues.isEmpty()) {
                pathList.addAll(mappingValues);
            } else {
                pathList.addAll(getAttributeValues(entry, "value")); // path
            }
            pathList.addAll(getAttributeValues(entry, "path")); // path
        }

        if (pathList.isEmpty()) {
            pathList.add(defaultPath); //没指定参数
        }

        if (methodList.size() > 0) {
            for (String method : methodList) {
                for (String path : pathList) {
                    requestPaths.add(new RequestPath(path, method, ""));
                }
            }
        } else {
            for (String path : pathList) {
                requestPaths.add(new RequestPath(path, null, ""));
            }
        }

        return requestPaths;
    }

    private List<String> getAttributeValues(KtAnnotationEntry entry, String attribute) {
        KtValueArgumentList valueArgumentList = entry.getValueArgumentList();

        if (valueArgumentList == null) {
            return Collections.emptyList();
        }

        List<KtValueArgument> arguments = valueArgumentList.getArguments();

        for (int i = 0; i < arguments.size(); i++) {
            KtValueArgument ktValueArgument = arguments.get(i);
            KtValueArgumentName argumentName = ktValueArgument.getArgumentName();

            KtExpression argumentExpression = ktValueArgument.getArgumentExpression();

            if ((argumentName == null && attribute == null) || (argumentName != null && argumentName.getText().equals(attribute))) {
                List<String> methodList = new ArrayList<>();
                // array, kotlin 1.1-
                if (argumentExpression.getText().startsWith("arrayOf")) {
                    List<KtValueArgument> pathValueArguments = ((KtCallExpression) argumentExpression).getValueArguments();
                    for (KtValueArgument pathValueArgument : pathValueArguments) {
                        methodList.add(pathValueArgument.getText().replace("\"", ""));
                    }
                    // array, kotlin 1.2+
                } else if (argumentExpression.getText().startsWith("[")) {
                    List<KtExpression> innerExpressions = ((KtCollectionLiteralExpression) argumentExpression).getInnerExpressions();
                    for (KtExpression ktExpression : innerExpressions) {
                        methodList.add(ktExpression.getText().replace("\"", ""));
                    }
                } else {
                    // 有且仅有一个value
                    PsiElement[] paths = ktValueArgument.getArgumentExpression().getChildren();
//                            Arrays.stream(paths).forEach(p -> methodList.add(p.getText()));
                    methodList.add(paths.length == 0 ? "" : paths[0].getText());
                }

                return methodList;
            }
        }

        return new ArrayList<>();
    }


    @Override
    public void getYapiRequest(Project project, YapiDTO YapiDTO, PsiMethod psiMethod) {
        PsiParameter[] psiParameters = psiMethod.getParameterList().getParameters();
        if (psiParameters.length > 0) {
            ArrayList list = new ArrayList<YapiQueryDTO>();
            List<YapiHeaderDTO> yapiHeaderDTOList = new ArrayList<>();
            List<YapiPathVariableDTO> yapiPathVariableDTOList = new ArrayList<>();
            for (PsiParameter psiParameter : psiParameters) {
                if (Consts.JAVA.HTTP_SERVLET_REQUEST.equals(psiParameter.getType().getCanonicalText()) || Consts.JAVA.HTTP_SERVLET_RESPONSE.equals(psiParameter.getType().getCanonicalText())) {
                    continue;
                }
                PsiAnnotation psiAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiParameter, SpringRequestParamAnnotations.REQUEST_BODY.getQualifiedName());
                if (psiAnnotation != null) {
                    YapiDTO.setRequestBody(getYapiResponse(project, psiParameter.getType()));
                } else {
                    psiAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiParameter, SpringRequestParamAnnotations.REQUEST_PARAM.getQualifiedName());
                    YapiHeaderDTO yapiHeaderDTO = null;
                    YapiPathVariableDTO yapiPathVariableDTO = null;
                    if (psiAnnotation == null) {
                        psiAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiParameter, SpringRequestParamAnnotations.REQUEST_ATTRIBUTE.getQualifiedName());
                        if (psiAnnotation == null) {
                            psiAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiParameter, SpringRequestParamAnnotations.REQUEST_HEADER.getQualifiedName());
                            if (psiAnnotation == null) {
                                psiAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiParameter, SpringRequestParamAnnotations.PATH_VARIABLE.getQualifiedName());
                                yapiPathVariableDTO = new YapiPathVariableDTO();
                            } else {
                                yapiHeaderDTO = new YapiHeaderDTO();
                            }
                        }
                    }
                    if (psiAnnotation != null) {
                        PsiNameValuePair[] psiNameValuePairs = psiAnnotation.getParameterList().getAttributes();
                        YapiQueryDTO yapiQueryDTO = new YapiQueryDTO();

                        if (psiNameValuePairs.length > 0) {
                            for (PsiNameValuePair psiNameValuePair : psiNameValuePairs) {
                                if ("name".equals(psiNameValuePair.getName()) || "value".equals(psiNameValuePair.getName())) {
                                    if (yapiHeaderDTO != null) {
                                        yapiHeaderDTO.setName(psiNameValuePair.getValue().getText().replace("\"", ""));
                                    } else if (yapiPathVariableDTO != null) {
                                        yapiPathVariableDTO.setName(psiNameValuePair.getValue().getText().replace("\"", ""));
                                    } else {
                                        yapiQueryDTO.setName(psiNameValuePair.getValue().getText().replace("\"", ""));
                                    }
                                } else if ("required".equals(psiNameValuePair.getName())) {
                                    yapiQueryDTO.setName(psiParameter.getName());
                                    yapiQueryDTO.setRequired(psiNameValuePair.getValue().getText().replace("\"", "").replace("false", "0").replace("true", "1"));
                                } else if ("defaultValue".equals(psiNameValuePair.getName())) {
                                    if (yapiHeaderDTO != null) {
                                        yapiHeaderDTO.setExample(psiNameValuePair.getValue().getText().replace("\"", ""));
                                    } else {
                                        yapiQueryDTO.setExample(psiNameValuePair.getValue().getText().replace("\"", ""));
                                    }
                                } else {
                                    if (yapiHeaderDTO != null) {
                                        yapiHeaderDTO.setName(StringUtils.isNotBlank(psiNameValuePair.getLiteralValue()) ? psiNameValuePair.getLiteralValue() : psiParameter.getName());
                                        // 通过方法注释获得 描述 加上 类型
                                        yapiHeaderDTO.setDesc(DesUtil.getParamDesc(psiMethod, psiParameter.getName()) + "(" + psiParameter.getType().getPresentableText() + ")");
                                    }
                                    if (yapiPathVariableDTO != null) {
                                        yapiPathVariableDTO.setName(StringUtils.isNotBlank(psiNameValuePair.getLiteralValue()) ? psiNameValuePair.getLiteralValue() : psiParameter.getName());
                                        // 通过方法注释获得 描述 加上 类型
                                        yapiPathVariableDTO.setDesc(DesUtil.getParamDesc(psiMethod, psiParameter.getName()) + "(" + psiParameter.getType().getPresentableText() + ")");
                                    } else {
                                        yapiQueryDTO.setName(StringUtils.isNotBlank(psiNameValuePair.getLiteralValue()) ? psiNameValuePair.getLiteralValue() : psiParameter.getName());
                                        // 通过方法注释获得 描述 加上 类型
                                        yapiQueryDTO.setDesc(DesUtil.getParamDesc(psiMethod, psiParameter.getName()) + "(" + psiParameter.getType().getPresentableText() + ")");
                                    }
                                    if (NormalTypes.normalTypes.containsKey(psiParameter.getType().getPresentableText())) {
                                        if (yapiHeaderDTO != null) {
                                            yapiHeaderDTO.setExample(NormalTypes.normalTypes.get(psiParameter.getType().getPresentableText()).toString());
                                        } else if (yapiPathVariableDTO != null) {
                                            yapiPathVariableDTO.setExample(NormalTypes.normalTypes.get(psiParameter.getType().getPresentableText()).toString());
                                        } else {
                                            yapiQueryDTO.setExample(NormalTypes.normalTypes.get(psiParameter.getType().getPresentableText()).toString());
                                        }
                                    } else {
                                        YapiDTO.setRequestBody(getYapiResponse(project, psiParameter.getType()));
                                    }

                                }
                            }
                        } else {
                            if (yapiHeaderDTO != null) {
                                yapiHeaderDTO.setName(psiParameter.getName());
                                // 通过方法注释获得 描述 加上 类型
                                yapiHeaderDTO.setDesc(DesUtil.getParamDesc(psiMethod, psiParameter.getName()) + "(" + psiParameter.getType().getPresentableText() + ")");
                            } else if (yapiPathVariableDTO != null) {
                                yapiPathVariableDTO.setName(psiParameter.getName());
                                // 通过方法注释获得 描述 加上 类型
                                yapiPathVariableDTO.setDesc(DesUtil.getParamDesc(psiMethod, psiParameter.getName()) + "(" + psiParameter.getType().getPresentableText() + ")");
                            } else {
                                yapiQueryDTO.setName(psiParameter.getName());
                                // 通过方法注释获得 描述 加上 类型
                                yapiQueryDTO.setDesc(DesUtil.getParamDesc(psiMethod, psiParameter.getName()) + "(" + psiParameter.getType().getPresentableText() + ")");
                            }
                            if (NormalTypes.normalTypes.containsKey(psiParameter.getType().getPresentableText())) {
                                if (yapiHeaderDTO != null) {
                                    yapiHeaderDTO.setExample(NormalTypes.normalTypes.get(psiParameter.getType().getPresentableText()).toString());
                                } else if (yapiPathVariableDTO != null) {
                                    yapiPathVariableDTO.setExample(NormalTypes.normalTypes.get(psiParameter.getType().getPresentableText()).toString());
                                } else {
                                    yapiQueryDTO.setExample(NormalTypes.normalTypes.get(psiParameter.getType().getPresentableText()).toString());
                                }
                            } else {
                                YapiDTO.setRequestBody(getYapiResponse(project, psiParameter.getType()));
                            }
                        }
                        if (yapiHeaderDTO != null) {
                            if (StringUtils.isNullOrEmpty(yapiHeaderDTO.getDesc())) {
                                // 通过方法注释获得 描述  加上 类型
                                yapiHeaderDTO.setDesc(DesUtil.getParamDesc(psiMethod, psiParameter.getName()) + "(" + psiParameter.getType().getPresentableText() + ")");
                            }
                            if (StringUtils.isNullOrEmpty(yapiHeaderDTO.getExample()) && NormalTypes.normalTypes.containsKey(psiParameter.getType().getPresentableText())) {
                                yapiHeaderDTO.setExample(NormalTypes.normalTypes.get(psiParameter.getType().getPresentableText()).toString());
                            }
                            yapiHeaderDTOList.add(yapiHeaderDTO);
                        } else if (yapiPathVariableDTO != null) {
                            if (StringUtils.isNullOrEmpty(yapiPathVariableDTO.getDesc())) {
                                // 通过方法注释获得 描述  加上 类型
                                yapiPathVariableDTO.setDesc(DesUtil.getParamDesc(psiMethod, psiParameter.getName()) + "(" + psiParameter.getType().getPresentableText() + ")");
                            }
                            if (StringUtils.isNullOrEmpty(yapiPathVariableDTO.getExample()) && NormalTypes.normalTypes.containsKey(psiParameter.getType().getPresentableText())) {
                                yapiPathVariableDTO.setExample(NormalTypes.normalTypes.get(psiParameter.getType().getPresentableText()).toString());
                            }
                            yapiPathVariableDTOList.add(yapiPathVariableDTO);
                        } else {
                            if (StringUtils.isNullOrEmpty(yapiQueryDTO.getDesc())) {
                                // 通过方法注释获得 描述 加上 类型
                                yapiQueryDTO.setDesc(DesUtil.getParamDesc(psiMethod, psiParameter.getName()) + "(" + psiParameter.getType().getPresentableText() + ")");
                            }
                            if (StringUtils.isNullOrEmpty(yapiQueryDTO.getExample()) && NormalTypes.normalTypes.containsKey(psiParameter.getType().getPresentableText())) {
                                yapiQueryDTO.setExample(NormalTypes.normalTypes.get(psiParameter.getType().getPresentableText()).toString());
                            }
                            list.add(yapiQueryDTO);
                        }
                    } else {
                        // 支持实体对象接收
                        YapiDTO.setReq_body_type("form");
                        if (YapiDTO.getReq_body_form() != null) {
                            YapiDTO.getReq_body_form().addAll(getYapiRequestForm(project, psiParameter, psiMethod));
                        } else {
                            YapiDTO.setReq_body_form(getYapiRequestForm(project, psiParameter, psiMethod));
                        }
                    }
                }
            }
            YapiDTO.setParams(list);
            YapiDTO.setHeader(yapiHeaderDTOList);
            YapiDTO.setReq_params(yapiPathVariableDTOList);
        }
    }

}
